﻿using AutoMapper;
using Blm.Utils.Extensions;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using VisionCloud.Core.Model.Enums.Schedule;
using VisionCloud.Core.Model.Rs;
using VisionCloud.Domain.Common;
using VisionCloud.Domain.DtoModel.Ms;
using VisionCloud.Domain.DtoModel.Rs;
using VisionCloud.Domain.DtoModel.Schedule.WorkOrder;
using VisionCloud.Domain.Repository.Interface.Ms;
using VisionCloud.Domain.Repository.Interface.Rs;
using VisionCloud.Domain.UnitOfWork;
using VisionCloud.Utility;

namespace VisionCloud.Service.Rs
{
    public class RsWorkOrderMapService
    {
        public IUnitOfWork UnitOfWork { get; set; }
        public IRsWorkOrderMapRepository RsWorkOrderMapRepository { get; set; }

        public IRsWorkOrderRepository RsWorkOrderRepository { get; set; }
        public IMsProjectRepository MsProjectRepository { get; set; }

        /// <summary>
        /// 获取工单对象
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<WorkOrderMapResponseDto> GetEntity(WorkOrderRequestDto dto)
        {
            var companyGuid = OperatorProvider.Get().CompanyGuid;
            return new WorkOrderMapResponseDto();
        }

        public async Task<ApiPageResult> GetWorkOrderMapProjects(WorkOrderMapRequestPageDto dto)
        {
            dto.CompanyId = OperatorProvider.Get().CompanyGuid;
            var result = await MsProjectRepository.GetMapProject(dto);
            return result;
        }

        /// <summary>
        /// 获取工单对象
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<ApiResult> Add(WorkOrderMapRequestDto dto)
        {
            var project = await MsProjectRepository.GetByIdAsync(dto.ProjectId);
            if (project == null || project.IsDeleted)
            {
                // 项目不存在不能绑定
                throw new OperationException("YB0463".SLocalize());
            }

            var workOrder = await RsWorkOrderRepository.GetByIdAsync(dto.WorkOrderId);
            if (workOrder == null || workOrder.IsDeleted)
            {
                // 工单不存在不能绑定
                throw new OperationException("YB0464".SLocalize());
            }

            if (workOrder.Status == WorkOrderStatusEnum.已完成)
            {
                // 工单已完成不能绑定
                throw new OperationException("YB0465".SLocalize());
            }

            var condition = new WorkOrderRequestDto()
            {
                WorkOrderId = dto.WorkOrderId,
                ProjectId = dto.ProjectId,
            };
            var companyGuid = OperatorProvider.Get().CompanyGuid;
            var workOrderMap = await RsWorkOrderMapRepository.GetEntity(condition, companyGuid);
            if (workOrderMap != null)
            {
                RsWorkOrderMapRepository.Remove(workOrderMap);
            }
            var model = new RsWorkOrderMap()
            {
                Id = Guid.NewGuid(),
                IsDeleted = false,
                CompanyGuid = companyGuid,
                ProjectId = dto.ProjectId,
                RsWorkOrderId = dto.WorkOrderId,
                BpsTaskId = workOrder.BpsTaskId,
            };
            RsWorkOrderMapRepository.Add(model);
            workOrder.Status = WorkOrderStatusEnum.未开始;
            RsWorkOrderRepository.Update(workOrder);
            
            // 更新项目状态未发布
            MsProjectRepository.UpdateProjectVersion(dto.ProjectId);
            await UnitOfWork.SaveChangesAsync();
            return ApiResult.Success();
        }


        /// <summary>
        /// 删除项目关联的工单
        /// </summary>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public async Task<ApiResult> Delete(Guid projectId)
        {
            var companyGuid = OperatorProvider.Get().CompanyGuid;
            var entities = await RsWorkOrderMapRepository.GetEntitiesByProject(projectId, companyGuid);
            RsWorkOrderMapRepository.RemoveRange(entities);
            await UnitOfWork.SaveChangesAsync();
            var result = new ApiResult()
            {
                Code = AjaxCode.Success,
                Result = string.Empty,
            };
            return result;
        }


        /// <summary>
        /// 获取项目对应工单
        /// </summary>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public async Task<List<RsWorkOrderMap>> GetEntities(Guid projectId)
        {
            var companyGuid = OperatorProvider.Get().CompanyGuid;
            var entities = await RsWorkOrderMapRepository.GetEntitiesByProject(projectId, companyGuid);
            return entities;
        }

        /// <summary>
        /// 根据项目楼栋楼层获取工单列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<ApiResult> GetWorkOrderListWithProjectInfo(GetWorkOrderListWithProjectInfoRqDto request)
        {
            List<GetWorkOrderListWithProjectInfoRspDto> list = await RsWorkOrderMapRepository.GetWorkOrderListWithProjectInfo(request);
            return new ApiResult
            {
                Code = AjaxCode.Success,
                Result = list
            };
        }
    }
}
